<template>
  <a-modal
    :destroy-on-close="true"
    :mask-closable="false"
    title="选择用户"
    :open="visible"
    :onCancel="onCancel"
    :width="800"
  >
    <template #footer>
      <a-button key="save" type="primary" @click="submitUser">确定</a-button>
      <a-button key="back" @click="() => onCancel()">取消</a-button>
    </template>
    <div class="layout-main-conent">
      <a-card
        :bordered="false"
        style="margin-bottom: 15px"
        :bodyStyle="{ paddingBottom: '0' }"
      >
        <a-form :labelCol="{ span: 6, offset: 0 }" :wrapper-col="{ span: 18 }">
          <a-row :gutter="16" justify="end">
            <a-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6">
              <a-form-item label="用户姓名：">
                <a-input
                  placeholder="请输入"
                  v-model:value="searchModelRef.username"
                />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6">
              <a-form-item label="域账号：">
                <a-input
                  placeholder="请输入"
                  v-model:value="searchModelRef.adAccount"
                />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6">
              <a-form-item label="科室：">
                <a-input
                  placeholder="请输入"
                  v-model:value="searchModelRef.orgName"
                />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6">
              <div class="text-align-right" style="padding-bottom: 24px">
                <a-button type="primary" @click="searchFormSubmit"
                  >查询</a-button
                >
                <a-button style="margin-left: 8px" @click="searchResetFields"
                  >重置</a-button
                >
              </div>
            </a-col>
          </a-row>
        </a-form>
      </a-card>
      <a-card :bordered="false">
        <a-table
          row-key="id"
          :columns="columns"
          :data-source="data"
          :loading="loading"
          :pagination="pagination"
          :row-selection="rowSelection"
        >
          <template #action="{ record }">
            <a-button type="link" @click="() => detailData(record.id)"
              >查看</a-button
            >
          </template>
        </a-table>
      </a-card>
    </div>
  </a-modal>
</template>
<script lang="ts">
export default {
  name: "UserSelectModal",
};
</script>
<script lang="ts" setup>
import { PropType, reactive, ref, defineProps, computed, onMounted } from "vue";
import { message, Form } from "ant-design-vue";
import { queryUserList } from "../service";
import { UserDataItem } from "../data.d";
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
  onCancel: {
    type: Function,
    required: true,
  },
  onSubmit: {
    type: Function,
    required: true,
  },
});
// 列表数据
const data = ref([]);
// 列表分页
const pagination = ref({
  current: 1,
  pageSize: 10,
  total: 0, // 假设总共有100条数据
  showTotal: (total) => `共 ${total} 条数据`,
  onChange: (page, pageSize) => {
    // 分页改变时的回调函数
    pagination.value.current = page;
    pagination.value.pageSize = pageSize;
    getUserList(page);
  },
  showSizeChanger: true,
  showQuickJumper: true,
});
const userList = ref<UserDataItem[]>([]);
const rowSelection = ref({
  checkStrictly: false,
  onChange: (
    selectedRowKeys: (string | number)[],
    selectedRows: UserDataItem[]
  ) => {
    userList.value = selectedRows;
    console.log(
      `selectedRowKeys: ${selectedRowKeys}`,
      "selectedRows: ",
      selectedRows
    );
  },
  onSelect: (
    record: UserDataItem,
    selected: boolean,
    selectedRows: UserDataItem[]
  ) => {
    console.log(record, selected, selectedRows);
  },
  onSelectAll: (
    selected: boolean,
    selectedRows: UserDataItem[],
    changeRows: UserDataItem[]
  ) => {
    console.log(selected, selectedRows, changeRows);
  },
});
// 列表字段
const columns = [
  {
    title: "用户姓名",
    dataIndex: "username",
    key: "username",
  },
  {
    title: "域账号",
    dataIndex: "adAccount",
    key: "adAccount",
  },
  {
    title: "科室",
    dataIndex: "orgName",
    key: "orgName",
  },
];

// 获取数据
const loading = ref<boolean>(true);

const submitUser = () => {
  console.log("userList.value", userList.value);
  if (userList.value.length > 0) {
    props.onSubmit(userList.value);
  } else {
    message.warning("至少请选择一个用户");
  }
};

// 搜索
const searchFormSubmit = async () => {
  getUserList(1);
};
const searchResetFields = async () => {
  searchModelRef.value = {
    username: "",
    adAccount: "",
    orgName: "",
  };
  searchFormSubmit();
};
// 表单值
const searchModelRef = ref({
  username: "",
  adAccount: "",
  orgName: "",
});
const getUserList = async (current: number) => {
  const params = {
    pageNum: current,
    pageSize: pagination.value.pageSize,
    ...searchModelRef.value,
  };
  loading.value = true;
  const res = await queryUserList(params);
  if (res.success) {
    data.value = res.data.records;
    pagination.value.total = res.data.total;
  }
  loading.value = false;
};
const detailData = async (id: string) => {
  try {
    message.warning("进行了搜索!");
  } catch (error: any) {
    message.warning(error);
  }
};

onMounted(() => {
  getUserList(1);
});
</script>
<style scoped lang="less">
:deep(.ant-card-head-title) {
  button {
    margin-right: 20px;
  }
}
</style>
./data
